<template>
	<view class="container">
		<view class="address-list">
			<view class="a-address" v-for="(item,index) in addressList" :key="index">
				<view class="left-text" :class="item.isDefault? 'active':''" @longtap="selectTap(item.id,index)">
					<view class="name-tel">
						{{item.name}} {{item.mobile}}
					</view>
					<view class="address-box">
						{{item.address}}
					</view>
				</view>
				<view class="right-edit" @tap="editAddess(item.id)"></view>
			</view>
		</view>
		<view class="bottom-box">
			<view class="add-btn" @tap="addAddess">新增收货地址</view>
		</view>

	</view>
</template>

<script>
	import { address } from '../../common/config'
    export default {
        data() {
            return {
                addressList: []
            }
        },
		onShow() {
            this.getAddress()
		},
        onLoad() {
        },
        methods: {
            selectTap(id, index) {
                // console.log("tap item id:" + id);
                uni.showModal({
                    title: '提示',
                    content: '确定删除本条收货地址信息吗？',
                    success:  (res) => {
                        if (res.confirm) {
                            // console.log('用户点击确定');
                            this.delAddress(id, index)
                        } else if (res.cancel) {
                            // console.log('用户点击取消');
                        }
                    }
                });
            },
            editAddess(id) {
                uni.navigateTo({
                    url:'/pages/address/edit?id=' + id
                })
            },
            addAddess() {
                uni.navigateTo({
                    url:'/pages/address/add'
                })
            },
			async delAddress(id, index) {
               await this.$api.MyRequest(address.DeleteAddress.url, address.DeleteAddress.method, {id})
                   .then(res => {
                       this.addressList.splice(index,1);
                       this.msg('删除成功！')
				   })
				   .catch(e => {
				   })
			},
			async getAddress() {
               await this.$api.MyRequest(address.GetAllAddress.url, address.GetAllAddress.method, {page:0})
				   .then(res => {
                       let data =[];
					   res.map((one) =>{
				           let address  =(one['province'] ? one['province']['name'] : '') + (one['city'] ? one['city']['name'] : '') + (one['region'] ? one['region']['name'] : '') + (one['receiver_address'] ? one['receiver_address'] : '');
						   let temp = {
						       id: one.id,
							   name: one.receiver_name,
							   mobile: one.receiver_phone,
							   isDefault:one.is_default,
							   address: address
						   };
						   data.push(temp);
                       });
				       this.addressList = data;
                       this.$forceUpdate();
				   })
				   .catch(err => {
				   })
			},
        }
    }
</script>

<style scoped>
	page {
		background: #fff;
	}
	.container {
		background-color: #fff;
		width: 100vw;
		height: 100vh;
		overflow-y: scroll;
	}

	.address-list {
		width: 100%;
		background-color: #fff;
		margin-top: 20upx;
		padding-bottom: 100upx;
	}

	.address-list .a-address {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30upx 0;
		border-bottom: 1upx solid #eee;
	}

	.a-address .left-text {
		width: 610upx;
		box-sizing: border-box;
		padding-left: 58upx;
	}

	.a-address .left-text.active {
		background: url() no-repeat left center;
		background-size: 28upx auto;
	}

	.a-address .left-text .name-tel {
		margin-bottom: 20upx;
	}

	.a-address .left-text .address-box {
		font-size: 24upx;
		color: #888888;
		line-height: 36upx;
	}

	.a-address .right-edit {
		width: 109upx;
		height: 100%;
		padding: 50upx 0 50upx 58upx;
		box-sizing: border-box;
		background: url() no-repeat 43upx center;
		background-size: 35upx auto;
	}

	.bottom-box {
		position: fixed;
		width: 100%;
		bottom: 0;
		font-size: 28upx;
		color: #000000;
		border-top: 1upx solid #eee;
		height: 100upx;
		line-height: 100upx;
		background: #fff url() no-repeat 705upx center;
		background-size: 15upx auto;
	}

	.bottom-box .add-btn {
		margin-left: 30upx;
		padding-left: 70upx;
		background: url() no-repeat left center;
		background-size: 40upx auto;
	}
</style>
